package solution;

/**
 * create by yongli on 2020-07-22 17:02
 */

public class SumTarget {
    public static void main(String[] args) {
        int[] a = {2,7,11,15};
        int target = 9;
        int target2 =13;
        int[] res = find(a,target);
        int[] res2 = find(a,target2);
        System.out.println("["+ res[0]+","+res[1]+"]");

    }

    // 采用两边向中间搜索
    public static int[] find(int[] nums, int target) {
        int[] res = {-1, -1};
        int start = 0;
        int end = nums.length - 1;

        while (start < end) {
            int sum = nums[start] + nums[end];
            if(sum == target){
                res[0] = start;
                res[1] = end;
                return res;
            }
            if (sum < target) {
                start++;
            }else {
                end--;
            }
        }
        return res;
    }
}
